/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.peng.rest;

import me.peng.annotation.Log;
import me.peng.domain.RecyclerAddress;
import me.peng.domain.config.SysResult;
import me.peng.service.RecyclerAddressService;
import me.peng.service.RecyclerUserService;
import me.peng.service.dto.RecyclerAddressQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import me.peng.annotation.rest.AnonymousDeleteMapping;
import me.peng.annotation.rest.AnonymousGetMapping;
import me.peng.annotation.rest.AnonymousPostMapping;
import me.peng.annotation.rest.AnonymousPutMapping;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author pengjianye
* @date 2021-09-30
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "/api/recyclerAddress管理")
@RequestMapping("/api/recyclerAddress")
@CrossOrigin
public class RecyclerAddressController {

    private final RecyclerAddressService recyclerAddressService;

    @Autowired
    RecyclerAddressService userAddressService;



    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('recyclerAddress:list')")
    public void download(HttpServletResponse response, RecyclerAddressQueryCriteria criteria) throws IOException {
        recyclerAddressService.download(recyclerAddressService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询/api/recyclerAddress")
    @ApiOperation("查询/api/recyclerAddress")
    @PreAuthorize("@el.check('recyclerAddress:list')")
    public ResponseEntity<Object> query(RecyclerAddressQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(recyclerAddressService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增/api/recyclerAddress")
    @ApiOperation("新增/api/recyclerAddress")
    @PreAuthorize("@el.check('recyclerAddress:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody RecyclerAddress resources){
        return new ResponseEntity<>(recyclerAddressService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改/api/recyclerAddress")
    @ApiOperation("修改/api/recyclerAddress")
    @PreAuthorize("@el.check('recyclerAddress:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody RecyclerAddress resources){
        recyclerAddressService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除/api/recyclerAddress")
    @ApiOperation("删除/api/recyclerAddress")
    @PreAuthorize("@el.check('recyclerAddress:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Integer[] ids) {
        recyclerAddressService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }



    //添加地址信息
    @AnonymousPostMapping("/addressinfo")
    public SysResult addressUserAdd(@RequestBody  RecyclerAddress recyclerAddress){
        return userAddressService.addressUser(recyclerAddress);
    }

    //parentID查询地址信息
    @AnonymousGetMapping("/selectAddressinfo")
    public SysResult selectAddressinfo(Integer parentId){
        return userAddressService.selectAddressUser(parentId);
    }

    /**
     * 修改
     * @return
     */
    @AnonymousPutMapping("/updateUserInfo")
    public SysResult updateUserInfo(@RequestBody RecyclerAddress user){

        return  userAddressService.updateAddressInfo(user);
    }
    //删除
    @AnonymousDeleteMapping("/deleteAddressInfo/{id}")
    public SysResult deleteUserInfo(@PathVariable Integer id){

        return  userAddressService.deleteAddressInfo(id);
    }


    //根据id查询数据
    @AnonymousGetMapping("/findIdAddressinfo/{id}")
    public SysResult findIdAddressinfo(@PathVariable Integer id){
        return userAddressService.selectIdAddress(id);
    }





}
